﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExtendedEventsManager
{
    public partial class ExpressionEditor : Form
    {
        public ExpressionEditor()
        {
            InitializeComponent();
        }

        public ExpressionEditor(List<string> fields, List<string> comparisons, string existingvalue)
        {
            InitializeComponent();
            this.listBox1.DataSource = fields;
            this.listBox2.DataSource = comparisons;
            this.textBox1.Text = existingvalue;
        }
        
        public string ExpressionValue
        {
            get { return this.textBox1.Text; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            AddToPredicate(listBox1.SelectedItem.ToString());
        }

        private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            AddToPredicate(listBox2.SelectedItem.ToString()+"( <source_column>, <value> )");
        }

        private void AddToPredicate(string valToAdd)
        {
            if (textBox1.Text.Length > 0 && !textBox1.Text.EndsWith(" "))
                textBox1.Text += " ";
            textBox1.Text += valToAdd;
            textBox1.Focus();
            textBox1.SelectionStart = textBox1.TextLength;
            textBox1.SelectionLength = 0;
        }

        private void btn_Operator_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            AddToPredicate(btn.Text);
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
